
package com.xiguthings.xiniu.iot.device.state.jedis.impl;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.xiguthings.xiniu.iot.common.DeviceData;
import com.xiguthings.xiniu.iot.common.DeviceDataBody;
import com.xiguthings.xiniu.iot.common.DeviceDataHead;
import com.xiguthings.xiniu.iot.device.state.jedis.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Pipeline;

/**
 * redis非集群使用的实现类
 */
public class JedisClientPool implements JedisClient {

	private JedisPool jedisPool;

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.set(key, value);
		jedis.close();
		return result;
	}

	@Override
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.get(key);
		jedis.close();
		return result;
	}

	@Override
	public Boolean exists(String key) {
		Jedis jedis = jedisPool.getResource();
		Boolean result = jedis.exists(key);
		jedis.close();
		return result;
	}

	@Override
	public Long expire(String key, int seconds) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.expire(key, seconds);
		jedis.close();
		return result;
	}

	@Override
	public Long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

	@Override
	public Long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	@Override
	public Long hset(String key, String field, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hset(key, field, value);
		jedis.close();
		return result;
	}

	@Override
	public String hget(String key, String field) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.hget(key, field);
		jedis.close();
		return result;
	}

	@Override
	public Long hdel(String key, String... field) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hdel(key, field);
		jedis.close();
		return result;
	}

	@Override
	public void pipelineHSets(Map<String, DeviceData> deviceDataMap) {

		Jedis jedis = jedisPool.getResource();
		Pipeline pipeline = jedis.pipelined();
		pipeline.multi();

		for (Map.Entry<String, DeviceData> mapEntry : deviceDataMap.entrySet()) {
			String hkey = mapEntry.getKey();
			DeviceData deviceData = mapEntry.getValue();
			DeviceDataBody deviceDataBody = deviceData.getBody();
			if (deviceData == null) {
				return;
			}
			// 把数据交给jedis
			if (StringUtils.isBlank(hkey)) {
				return;
			}

			String ts = deviceData.getHead().getTs().toString();// 数据的时间戳

			// -------- 存储设备和设备代理网关关系---------
			DeviceDataHead head = deviceData.getHead();
			if (StringUtils.isNoneBlank(head.getProxyId())) {
				pipeline.hset(head.getProxyId(), head.getDeviceId().toString(), ts);
			}

			// 分隔符号
			String separator = ",";
			// 需要保存的head中的字段
			Set<String> keySet = deviceData.getHead().keySet();
			// String[] headKeyArr = new String[] { "proxy_id","sn","domain_id",};
			for (String hfield : keySet) {
				Object hvalue = deviceData.getHead().get(hfield);
				if (hvalue == null) {
					continue;
				}

				String hvalueStr = hvalue.toString();
				if (StringUtils.isBlank(hvalueStr)) {
					continue;
				}
				pipeline.hset(hkey, "_" + hfield, hvalueStr);
			}
			// --------- body中的字段存储-------
			Set<Map.Entry<String, Object>> bodyEntrys = deviceDataBody.entrySet();
			for (Map.Entry<String, Object> entry : bodyEntrys) {
				String hfield = entry.getKey();
				Object fieldValue = entry.getValue();
				if (fieldValue == null) {
					continue;
				}
				String hvalue = ts + separator + fieldValue;
				pipeline.hset(hkey, hfield, hvalue);
			}
		}

		pipeline.exec();
		pipeline.syncAndReturnAll();
		try {
			pipeline.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			jedis.close();
		}

	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}
}
